const utils = {
	server () {
		const hostname = location.hostname;
		let server = '';
		if (hostname === 'wx.adbaitai.com') {
			server = 'https://wx.adbaitai.com';
		} else if (hostname === 'wxpre.adbaitai.com') {
			server = 'https://wxpre.adbaitai.com';
		} else {}
		console.log('server:' + server);
		return server;
	},
	changeTitle(title) {
		if (title === undefined) {
			return
		}
		// alert(title);
		document.title = title
		var mobile = navigator.userAgent.toLowerCase()
		if (/iphone|ipad|ipod/.test(mobile)) {
			var iframe = document.createElement('iframe');
				iframe.style.display = 'none'
				// 替换成站标favicon路径或者任意存在的较小的图片即可
				iframe.setAttribute('src', '/favicon.ico')
			var iframeCallback = function () {
				setTimeout(function () {
				iframe.removeEventListener('load', iframeCallback)
				document.body.removeChild(iframe)
				}, 0)
			}
			iframe.addEventListener('load', iframeCallback)
			document.body.appendChild(iframe)
		}
	},
	formatDate (date, fmt) {
		var o = {
			"M+": date.getMonth() + 1, //月份
			"d+": date.getDate(), //日
			"h+": date.getHours(), //小时
			"m+": date.getMinutes(), //分
			"s+": date.getSeconds(), //秒
			"q+": Math.floor((date.getMonth() + 3) / 3), //季度
			"S": date.getMilliseconds() //毫秒
		};
		if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (date.getFullYear() + "").substr(4 - RegExp.$1.length));
		for (var k in o)
			if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
		return fmt;
	},
	isObject (o) {
		return typeof o === 'object' && Object.prototype.toString.call(o) === '[object Object]';
	},
	getJsonData (o, k) {
		return utils.isObject(o) && typeof k === 'string' && o[k] && o[k].success && o[k].data && o[k].data.length ? o[k].data : null;
	},
	addKey (o, k, v) {
		if (!Array.isArray(o) && !utils.isObject(o)) return;
		if (Array.isArray(o)) {
			o.forEach((item, index) => {
				item[k] = v;
			})
			return o;
		}
		return o[k] = v;
	},
	parse (str) {
		if (typeof str === 'string') {
			return JSON.parse(str);
		}
		return str;
	},
	stringify (obj) {
		if (utils.isObject(obj)) {
			return JSON.stringify(obj);
		}
		return obj;
	},
  getQueryString (url, name) {
    const reg = new RegExp('(^|\\?|&)' + name + '=([^&#]*)(\\s|&|#|$)','i');
    if(reg.test(url)) return RegExp.$2.replace(/\+/g, ' ');
  }
};

export default utils;
